/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ua.yandex.skipass;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 *
 * @author vlad
 */
public class PassByTime extends Skipass {

    private final PassType type;
    public final LocalDateTime startTime;
    public final LocalDateTime endTime;

    PassByTime(PassType type, LocalDateTime startTime, LocalDateTime endTime,
            SkipassSystem system) {
        super(system);
        this.type = type;
        this.startTime = startTime;
        this.endTime = endTime;
    }

    @Override
    public boolean validate() {
        if (this.getType() != PassType.SEASON
                && this.getType() != system.getTodayPassType()) {
            return false;
        }
        return LocalDateTime.now().isAfter(startTime)
                && LocalDateTime.now().isBefore(endTime);
    }

    @Override
    public void useCard() {
        if (!validate()) {
            system.incDeniedByTime(getType().ordinal());
            return;
        }
        if (isBlocked()) {
            system.incDeniedByTime(getType().ordinal());
            return;
        }
        system.incAllowedByTime(getType().ordinal());
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 73 * hash + Objects.hashCode(this.getType());
        hash = 73 * hash + Objects.hashCode(this.startTime);
        hash = 73 * hash + Objects.hashCode(this.endTime);
        hash = 73 * hash + Objects.hashCode(this.getUuid());
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        PassByTime other = (PassByTime) obj;
        if (!Objects.equals(this.getUuid(), other.getUuid())) {
            return false;
        }
        if (this.getType() != other.getType()) {
            return false;
        }
        if (!Objects.equals(this.startTime, other.startTime)) {
            return false;
        }
        return !Objects.equals(this.endTime, other.endTime);
    }

    /**
     * @return the type
     */
    public PassType getType() {
        return type;
    }

}
